package com.ce.pms.system.custservice.service.impl;

import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.IReturnVisitItemDao;
import com.ce.pms.system.custservice.model.OptionsDef;
import com.ce.pms.system.custservice.model.ReturnVisitItemDO;
import com.ce.pms.system.custservice.service.IReturnVisitItemService;
import com.ce.pms.system.custservice.vo.ReturnVisitItemVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 描述：回访项目service实现
 * @author 王浩羽
 *
 */
public class ReturnVisitItemServiceImpl extends AbstractCustomerEventServiceImpl<ReturnVisitItemVO, ReturnVisitItemDO>  implements IReturnVisitItemService
{
	private IReturnVisitItemDao returnVisitItemDao;

	public boolean addReturnVisitItem(ReturnVisitItemVO returnVisitItemVO)
	{
		if(!BlankUtil.isBlank(returnVisitItemVO) && !BlankUtil.isBlank(returnVisitItemVO.getName()))
		{
			if(isNameExisit(returnVisitItemVO.getName())){
				throw new BusinessException("custservice:system_name_exist_exception",BusinessException.WARNING);
			}
			ReturnVisitItemDO returnVisitItemDO = assembleV2D(returnVisitItemVO,null);
			String[] codes = new String[]{"A","B"};
			String[] descriptions = new String[]{"是","否"};
			addDefaultOptionsDefs(returnVisitItemDO, codes, descriptions);
			returnVisitItemDO.setSysInfo(createInfo(returnVisitItemVO.getCreator()));
			return returnVisitItemDao.saveObject(returnVisitItemDO) != null;
		}
		return false;
	}

	private ReturnVisitItemDO addDefaultOptionsDefs(ReturnVisitItemDO returnVisitItemDO, String[] codes, String[] descriptions){
		if(codes.length != descriptions.length)
		{
			return returnVisitItemDO;
		}
		int ARRAY_SIZE = codes.length;
		for(int i = 0; i < ARRAY_SIZE; i++)
		{
			OptionsDef optionsDef = new OptionsDef();
			optionsDef.setCode(codes[i]);
			optionsDef.setDescription(descriptions[i]);
			returnVisitItemDO.getOptionsDefSet().add(optionsDef);
		}
		return returnVisitItemDO;
	}

	public boolean deleteReturnVisitItemById(Long returnVisitItemId)
	{
		if(returnVisitItemId==null)
		{
			return false;
		}
		try
		{
			returnVisitItemDao.deleteObjectById(returnVisitItemId);
			return true;
		}
		catch(RuntimeException e)
		{	//如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("custservice:system_delete_inused_exception",BusinessException.WARNING);
		}
	}

	public boolean editReturnVisitItem(ReturnVisitItemVO returnVisitItemVO)
	{
		if(!BlankUtil.isBlank(returnVisitItemVO) && !BlankUtil.isBlank(returnVisitItemVO.getKeyword()))
		{
			ReturnVisitItemDO returnVisitItemDO = returnVisitItemDao.findById(returnVisitItemVO.getKeyword());
			if(returnVisitItemDO==null)
			{
				return false;
			}
			returnVisitItemDO.setRemark(returnVisitItemVO.getRemark());
			returnVisitItemDO.setSequence(returnVisitItemVO.getSequence());
			returnVisitItemDO.setClazz(returnVisitItemVO.getClazz());
			returnVisitItemDO.setName(returnVisitItemVO.getName());
			returnVisitItemDO.setValidity(returnVisitItemVO.getValidity());
			modifyInfo(returnVisitItemDO.getSysInfo(), returnVisitItemVO.getModifier());
			return returnVisitItemDao.updateObject(returnVisitItemDO) != null;
		}
		return false;
	}

	protected boolean isNameExisit(String name){
		return returnVisitItemDao.findUniqueByName(name)!=null;
	}

	public List<ReturnVisitItemVO> queryAllReturnVisitItem()
	{
		List<ReturnVisitItemDO> returnVisitItemDOList = returnVisitItemDao.findAll();
		sortList(returnVisitItemDOList);
		return assembleDOList2VOList(returnVisitItemDOList);
	}
	   /**
	    * 功能描述:查询所有有效的回访项目<br>
	    * @return
	    * Create author:yixianjian <br>
	    * Create on:2009-1-16  <br>
	    */
	@SuppressWarnings("unchecked")
	public List<ReturnVisitItemVO> queryAllReturnVisitItemWithValidity(){
		List<ReturnVisitItemDO> returnVisitItemDOList = returnVisitItemDao.crtieriaFindBy("validity", "1").list();
		sortList(returnVisitItemDOList);
		return assembleDOList2VOList(returnVisitItemDOList);
	}

	public Page queryReturnVisitItemListByPage(SearchVO searchVO)
	{
		return queryVOListByPage(searchVO);
	}

	public void setReturnVisitItemDao(IReturnVisitItemDao returnVisitItemDao)
	{
		this.returnVisitItemDao = returnVisitItemDao;
	}

	@Override
	protected ReturnVisitItemDO findObjectDOById(Long id)
	{
		return returnVisitItemDao.findById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{
		Page page = returnVisitItemDao.queryByPage(searchVO);
		List<ReturnVisitItemDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}
	
	@Override
	protected int compareDO(ReturnVisitItemDO o1, ReturnVisitItemDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}
	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "custservice:system_name_exist_exception";
	}
}
